import { UITransform, v3, Vec3 } from "cc";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { smc } from "../../../common/ecs/model/SingletonModuleComp";
import { RoleModelComp } from "../model/RoleModelComp";
import { RoleMoveModelComp } from "../model/RoleMoveModelComp";
import { Role } from "../Role";

/** 关卡进入系统 */
@ecs.register("RoleMove")
export class RoleMoveSystem
  extends ecs.ComblockSystem
  implements ecs.ISystemUpdate
{
  filter(): ecs.IMatcher {
    return ecs.allOf(RoleMoveModelComp, RoleModelComp);
  }
  update(role: Role, dt: number): void {
    // 角色移动
    this.roleMove(role);
  }

  roleMove(role: Role) {
    let velocity = role.RoleMoveModel.velocity;
    if (velocity.length() > 0) {
      const wPos = role.RoleView.node
        .getComponent(UITransform)
        .convertToWorldSpaceAR(v3());
      velocity = smc.level.map.resetVelocity(wPos, velocity);
      role.RoleView.node.translate(velocity);
      // role.RoleView.node.position.add(v3(velocity.x, velocity.y, 0));
      if (velocity.length() === 0) return;
      // 修改方向
      let angle = role.RoleMoveModel.angle;
      role.RoleView.node
        .getChildByName("body")
        .setRotationFromEuler(0, 0, angle - 90);
    }
  }
}
